from Crypto.Cipher import AES
import base64
import requests

url = "https://music.163.com/api/comment/resource/comments/get?csrf_token="

data = '{"rid":"A_DJ_1_2529059377","threadId":"A_DJ_1_2529059377","pageNo":"1","pageSize":"20","cursor":"-1","offset":"0","orderType":"1","csrf_token":""}'

e = "010001"
f = "00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
g = "0CoJUm6Qyw8W8jud"
i = "9XMTDjbDvFLvg2LL"


def get_encSecKey():
    return "8e6ca1b09633cb399535d579bc55284bff21550ab3f0faa4b2f3e90a7e127d6a6d6075557ff0c961141b6a6a0f46c563e0e110829317ef30585d771aa9a507f99aeb7a2a676a11ec5891e9120e298a6d6d153eff7294b530a481525fe9b08546cd10043d460b474e359bbd2761ca2840e547b72d9183c167720f2c78a86b2452"


def get_params():
    fir = encrypt(data, g)
    sec = encrypt(fir, i)
    return sec


def encrypt(a, b):
    a = to_16mul(a)
    aes = AES.new(mode=AES.MODE_CBC, iv="0102030405060708".encode('utf-8'), key=b.encode('utf-8'))
    # ValueError: Data must be padded to 16 byte boundary in CBC mode
    bs = aes.encrypt(a.encode('utf-8'))
    return str(base64.b64encode(bs), 'utf-8')


def to_16mul(da):
    p = 16 - len(da) % 16
    da += chr(p) * p
    return da


if __name__ == '__main__':
    response = requests.post(url, data={
        "params": get_params(),
        "encSecKey": get_encSecKey()
    })
    print(response.text)
    # print(get_params())
    # print(get_encSecKey())
